package rwt.server.handlers;

import rwt.server.*;
import rwt.server.RailroadMessages.PutTrainInformationMsg;
import rwt.server.WebMessages.PutTrainInfo;
import rwt.server.core.*;
import rwt.server.railroad.Train;

public class PutTrainInformationHandler extends MessageHandler<PutTrainInformationMsg>
{
	@Override
	public void handle(PutTrainInformationMsg message, IConnection source) throws Exception
	{
		int trainId = RailServer.getTrainManager().slotToTrainId(message.slot);
		
		// Update web server's object for this train to reflect new info
		Train train = RailServer.getTrainManager().getTrainById(trainId);
		train.setSpeed(message.speed);
		train.setDirection(message.direction);
		train.setLight(message.light);
		train.setBell(message.bell);
		train.setHorn(message.horn);
		train.setMute(message.mute);
		
		// Create the web version of this message, and send to web client
		PutTrainInfo webMsg = new PutTrainInfo();
		webMsg.trainId = trainId;
		webMsg.speed = message.speed;
		webMsg.direction = message.direction;
		webMsg.light = message.light;
		webMsg.bell = message.bell;
		webMsg.horn = message.horn;
		webMsg.mute = message.mute;
		
		// TODO: Send only to the client (if any) who has the throttle controlling this train
		RailServer.getClientManager().sendToAll(webMsg);
	}
}
